

// 资源清单


function changePageButton(currentPage, change) {
    let pageButtons = ""
    // let pageButtonState;
    console.log(date_pageSum)
    if (currentPage === undefined) {
        if (date_pageSum < 7) {
            for (let i = 0; i < date_pageSum; i++) {
                let onePageButton = ''
                if (i === 0) {
                    onePageButton = '<button id="page-button-' + (i + 1) + '" class="page-button selected-page-button">' + (i + 1) + '</button>'
                } else {
                    onePageButton = '<button id="page-button-' + (i + 1) + '" class="page-button">' + (i + 1) + '</button>'
                }
                pageButtons += onePageButton;
            }
        } else {
            pageButtons =
                '                <button id="page-button-1" class="page-button ">1</button>' +
                '                <button id="page-button-2" class="page-button ">2</button>' +
                '                <button id="page-button-3" class="page-button ">3</button>' +
                '                <button id="page-button-4" class="page-button ">4</button>' +
                '                <button id="page-button-5" class="page-button ">...</button>' +
                '                <button id="page-button-6" class="page-button ">' + date_pageSum + '</button>'
            pageButtonState = 0
        }
    } else {
        if (date_pageSum - currentPage <= 3 && change === 1) {
            // 展开右侧省略号
            console.log("展开右侧省略号")
            $.each($(".page-controller").children(), function (index, item) {
                let onePageButton = ''
                if (index > 2 && index < 6) {
                    onePageButton =
                        '<button id="page-button-' + index + '" class="page-button ">' + (index + date_pageSum - 6) + '</button>'
                }
                pageButtons += onePageButton;
            })
            pageButtons = '                <button id="page-button-1" class="page-button ">1</button>' +
                '                <button id="page-button-2" class="page-button ">...</button>' + pageButtons +
                '                <button id="page-button-6" class="page-button ">' + date_pageSum + '</button>'
            pageButtonState = 1
        } else if (currentPage === date_pageSum && change > 1) {
            // 展开右侧省略号
            console.log("最后一页")
            $.each($(".page-controller").children(), function (index, item) {
                let onePageButton = ''
                if (index > 2 && index < 6) {
                    console.log(item.textContent)
                    onePageButton =
                        '<button id="page-button-' + index + '" class="page-button ">' + (index + date_pageSum - 6) + '</button>'
                }
                pageButtons += onePageButton;
            })
            pageButtons = '                <button id="page-button-1" class="page-button ">1</button>' +
                '                <button id="page-button-2" class="page-button ">...</button>' + pageButtons +
                '                <button id="page-button-6" class="page-button ">' + date_pageSum + '</button>'
            pageButtonState = 1
        } else if (currentPage > 3 && change === -1 && pageButtonState === 1) {
            console.log("点左，左移")
            $.each($(".page-controller").children(), function (index, item) {
                let onePageButton = ''
                if (index > 0 && index < 5) {
                    onePageButton =
                        '<button id="page-button-' + index + '" class="page-button ">' + (currentPage - 4 + index) + '</button>'
                }
                pageButtons += onePageButton;
            })
            pageButtons = pageButtons +
                '                <button id="page-button-5" class="page-button ">...</button>' +
                '                <button id="page-button-6" class="page-button ">' + date_pageSum + '</button>'
            pageButtonState = 0
        } else {
            // 点在右
            $.each($(".page-controller").children(), function (index, item) {
                let onePageButton = ''
                if (index > 0 && index < 5) {
                    onePageButton =
                        '<button id="page-button-' + index + '" class="page-button ">' + (parseInt(item.textContent) + change) + '</button>'
                }
                pageButtons += onePageButton;
            })
            pageButtons = pageButtons +
                '                <button id="page-button-5" class="page-button ">...</button>' +
                '                <button id="page-button-6" class="page-button ">' + date_pageSum + '</button>'
            pageButtonState = 0
        }


    }


    $(".page-controller").html('<button id="page-button-0" class="page-button "><</button>'
        + pageButtons +
        '<button id="page-button-7" class="page-button ">></button>')

}

$(document).on('click', ".page-controller button", function (event) {
    // 获取点击事件的目标元素
    var clickedElement = event.target;
    // 获取元素的类名
    var id = clickedElement.id;
    // 输出类名
    console.log($(".selected-page-button").attr("id"))
    let currentPage = parseInt($(".selected-page-button").text())
    let currentButton = parseInt($(".selected-page-button").attr("id").slice(-1))
    if (clickedElement.textContent === "...") {
        console.log("更多")
    } else if (clickedElement.textContent === "<") {
        console.log("上一页")

        if (currentPage > 1) {
            if (currentButton > 1 && pageButtonState === 0) {
                changePageNum("page-button-" + (currentButton - 1), currentPage - 1)
            } else if (pageButtonState === 1) {
                if (currentButton > 3) {
                    changePageNum("page-button-" + (currentButton - 1), currentPage - 1)
                } else {
                    if (date_pageSum >= 7) {
                        changePageButton(currentPage, -1)
                    }
                    changePageNum("page-button-" + currentButton, currentPage - 1)
                }
            } else {
                if (date_pageSum > 7) {
                    changePageButton(currentPage, -1)
                }
                changePageNum("page-button-" + currentButton, currentPage - 1)
            }
        }

    } else if (clickedElement.textContent === ">") {
        console.log("下一页")
        let currentPage = parseInt($(".selected-page-button").text())

        let currentButton = parseInt($(".selected-page-button").attr("id").slice(-1))
        if (currentPage < date_pageSum) {
            if (currentButton < 4 && pageButtonState === 0) {
                changePageNum("page-button-" + (currentButton + 1), currentPage + 1)
            } else if (currentButton < 7 && pageButtonState === 1) {
                changePageNum("page-button-" + (currentButton + 1), currentPage + 1)
            } else {

                if (date_pageSum >= 7) {
                    changePageButton(currentPage, +1)
                    changePageNum("page-button-" + currentButton, currentPage + 1)
                } else {
                    changePageNum("page-button-" + (currentButton + 1), currentPage + 1)
                }
            }
        }
    } else {
        if (id === "page-button-6" && pageButtonState === 0) {
            changePageButton(date_pageSum, 2)
        } else if (id === "page-button-1" && pageButtonState === 1) {
            changePageButton()
        }
        changePageNum(id, clickedElement.textContent)
    }

})

function changePageNum(id, currentPage) {
    $(".selected-page-button").removeClass("selected-page-button");
    $("#" + id).addClass("selected-page-button");
    date_currentPage = currentPage
    // tipZiyuan()
    $(".table_ziyan").empty()
    $(".table_ziyan").append(
        "                <tr>" +
        "                   <td class='ziyuan_td_left' style='width: 80px; font-size: 18px'>序号</td>" +
        "                   <td  style='font-size: 18px'>" + table_title + "</td>" +
        "                   <td style='width: 80px;font-size: 18px '>操作</td>" +
        "                </tr>");
    $.each(page_date_json.slice(9 * (date_currentPage - 1), 9 * date_currentPage), function (index, item) {
        let template =
            "                <tr>" +
            "                    <td class='td-ziyuan1'> ? </td>" +
            "                    <td class='td-ziyuan2'>?</td>" +
            "                    <td class='td-view'>查看</td>" +
            "                </tr>"
        template = template.format(index + date_currentPage * 9 - 8, item.CONTENT);
        $(".table_ziyan").append(template);
    })
}

let table_id = ""
let table_title = ""
let date_pageSum = 0
let date_currentPage = 0
let page_date_json = []
let pageButtonState = 0
function initTable(id, title, date) {
    table_id = id
    table_title = title
    page_date_json = date
    date_pageSum = Math.ceil(page_date_json.length / 9)
    changePageButton()
    changePageNum("page-button-1", 1)
}